/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network;

import lib.DOMNode;
import ru.qiwi.lib.UI.PUIEx;
import ru.qiwi.security.Security;
import Core.Contants;
import Core.GlobalData;
import Core.PaymentsLogic;
import DTO.OfflinePayment;
import DTO.PurchaseOnline;

/**
 * 
 * @author Minh Hung
 */
public class CreateXMLProvider {

	Object data;

	public CreateXMLProvider() {
	}

	public CreateXMLProvider(Object data) {
		this.data = data;
	}

	public String toXML(int type) {
		DOMNode request = new DOMNode("request");

		// request.addNode(GlobalData.auth.toXML());
		request.addNode(GlobalData.client.toXML());

		switch (type) {
		case Packet.TYPE_GETBALANCE_LOGIN:
		case Packet.TYPE_GETBALANCE_UPDATE:
			request.addNode("agents").addNode("getBalance");
			break;
		case Packet.TYPE_CANCELPAYMENT:
		case Packet.TYPE_GETGROUPS:
			request.addNode("providers").addNode("getGroups");
			break;
		case Packet.TYPE_GETPROVIDERS:
			request.addNode("providers").addNode("getUIProviders");
			break;
		case Packet.TYPE_CHANGEPASS:
			request.addNode("persons").addNode("changePassword")
					.addNode("new-password", "123455");
			break;
		case Packet.TYPE_SETPUBLICKEY: {
			DOMNode node = request.addNode("persons").addNode("setPublicKey");
			node.addNode("store-type", "4");
			node.addNode("pubkey", Security.getIns().getPublicKeyAsBase64());
		}
			break;
		case Packet.TYPE_SENDPAYMENTS:
			toXMLAddOffinePayment(request);
			break;
		case Packet.TYPE_GETPAYMENTSTATUS:
			toXMLGetOfflinePaymentStatus(request);
			break;
		case Packet.TYPE_GETMESSAGES:
			request.addNode("terminals").addNode("getMessages");
			break;
		case Packet.TYPE_GETRESULTCODES:
			request.addNode("system").addNode("getResultCodes");
			break;
		case Packet.TYPE_GETNOMENCLATURE:
			toXMLGetNomenclature(request);
			break;
		case Packet.TYPE_PURCHASEONLINE:
			toXMLPurchaseOnline(request);
			break;
		case Packet.TYPE_CHECKREQUISITES:
			toXMLCheckRequisites(request);
			break;
		case Packet.TYPE_GETAGENTS: {
			request.addNode("agents")
					.addNode("getAgents")
					.addNode("target-agent",
							String.valueOf(GlobalData.balance.getAgent_id()));
		}
			break;
		}

		return Contants.HEADER_XML + request.toString();
	}

	
	protected void toXMLAddOffinePayment(DOMNode request) {
		OfflinePayment payment = (OfflinePayment) data;
		request.addNode("providers").addNode("addOfflinePayment")
				.addNode(payment.toXML());
	}

	protected void toXMLGetOfflinePaymentStatus(DOMNode request) {
		OfflinePayment payment = PaymentsLogic.getIns().getLastQueue();

		DOMNode paymentNode = new DOMNode("payment");
		paymentNode.addAttribute("id", payment.getId());
		request.addNode("providers").addNode("getPaymentStatus")
				.addNode(paymentNode);
	}

	//

	protected void toXMLGetNomenclature(DOMNode request) {
		DOMNode nomenclature = new DOMNode("getNomenclature");
		if (data != null) {
			PUIEx pex = (PUIEx) data;
			nomenclature.addNode("service", pex.id);
			nomenclature.addNode("includeNested", "true");
		}
		request.addNode("providers").addNode(nomenclature);

	}

	protected void toXMLPurchaseOnline(DOMNode request) {
		request.addNode("providers").addNode("purchaseOnline")
				.addNode(((PurchaseOnline) data).toXML());
	}

	protected void toXMLCheckRequisites(DOMNode request) {
		OfflinePayment payment = null;
		if (data != null) {
			Object[] a = (Object[]) data;
			payment = (OfflinePayment) a[0];
			request.addNode("providers").addNode("checkPaymentRequisites")
					.addNode(payment.toXML());
		}

	}
}
